home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch07
/
fig07_02.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
1KB
|
45 lines
1 // Fig. 7.2: fig07_02.cpp
2 // Using a member initializer to initialize a
3 // constant of a built-in data type.
4
5 #include <iostream.h>
6
7 class Increment {
8 public:
9 Increment( int c = 0, int i = 1 );
10 void addIncrement() { count += increment; }
11 void print() const;
12
13 private:
14 int count;
15 const int increment;// const data member
16 };
17
18 // Constructor for class Increment
19 Increment::Increment( int c, int i )
20 : increment( i ) // initializer for const member
21 { count = c; }
22
23 // Print the data
24 void Increment::print() const
25 {
26 cout << "count = " << count
27 << ", increment = " << increment << endl;
28 }
29
30 int main()
31 {
32 Increment value( 10, 5 );
33
34 cout << "Before incrementing: ";
35 value.print();
36
37 for ( int j = 0; j < 3; j++ ) {
38 value.addIncrement();
39 cout << "After increment " << j << ": ";
40 value.print();
41 }
42
43 return 0;
44 }